package com.xingfudazi.app.service.impl;

import java.util.List;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.xingfudazi.app.domain.*;
import com.xingfudazi.app.enums.MemberTypeEnum;
import com.xingfudazi.app.enums.MemberVipEnum;
import com.xingfudazi.app.mapper.AppMemberMapper;
import com.xingfudazi.app.service.IAppMemberInfoService;
import com.xingfudazi.app.service.IAppMemberService;
import com.xingfudazi.app.service.ImUserService;
import com.xingfudazi.common.enums.DBDataStatus;
import com.xingfudazi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-21
 */
@Service
public class AppMemberServiceImpl extends ServiceImpl<AppMemberMapper,AppMember> implements IAppMemberService {
    @Autowired
    private AppMemberMapper appMemberMapper;

    @Autowired
    private IAppMemberInfoService appMemberInfoService;

    @Autowired
    private ImUserService imUserService;

    /**
     * 查询会员
     *
     * @param id 会员主键
     * @return 会员
     */
    @Override
    public AppMember selectAppMemberById(Long id) {
        return appMemberMapper.selectById(id);
    }

    /**
     * 查询会员列表
     *
     * @param appMember 会员
     * @return 会员
     */
    @Override
    public List<AppMember> selectAppMemberList(AppMember appMember) {
        LambdaQueryWrapper<AppMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(AppMember::getCreateTime);
        return appMemberMapper.selectList(wrapper);
    }

    /**
     * 新增会员
     *
     * @param appMember 会员
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertAppMember(AppMember appMember) {
        int insert = appMemberMapper.insert(appMember);

        if (insert  ==1) {
            imUserService.saveByAppMember(appMember);
        }
        return insert;
    }

    /**
     * 修改会员
     *
     * @param appMember 会员
     * @return 结果
     */
    @Override
    public int updateAppMember(AppMember appMember) {
        return appMemberMapper.updateById(appMember);
    }

    /**
     * 删除会员信息
     *
     * @param id 会员主键
     * @return 结果
     */
    @Override
    public int deleteAppMemberById(Long id) {
        AppMember appMember = new AppMember();
        appMember.setDelFlag(DBDataStatus.DELETED.getCode());
        appMember.setId(id);
        return appMemberMapper.updateById(appMember);
    }

    @Override
    public void init(AppAccount appAccount) {
        String phone = appAccount.getPhone();

        if (StringUtils.isBlank(phone)) {
            throw new IllegalArgumentException();
        }

        String memberType = appAccount.getMemberType();
        MemberTypeEnum memberTypeEnum;
        String nickName = "会员";
        if (StringUtils.isNotBlank(memberType)) {
            memberTypeEnum = MemberTypeEnum.valueOf(memberType);
            if (memberTypeEnum == MemberTypeEnum.MATCHMAKER) {
                nickName = "红娘";
            } else if (memberTypeEnum == MemberTypeEnum.COACH) {
                nickName = "教练";
            }
        }

        AppMember appMember = new AppMember();
        appMember.setAid(appMember.getId());
        appMember.setNickName(nickName + phone.substring(phone.length() - 4));
        appMember.setRanks(1);
        appMember.setPhone(phone);
        appMember.setCode(UUID.randomUUID().toString().replaceAll("-", ""));
        appMember.setAid(appAccount.getId());
        appMember.setExperience(0);
        appMember.setGc(0);
        appMember.setType(memberType == null ? MemberTypeEnum.MEMBER.name() : memberType);
        appMember.setVip(MemberVipEnum.VIP.name());
        this.insertAppMember(appMember);

        // 初始化个人信息
        appMemberInfoService.init(phone, nickName, appMember.getId());
    }


    @Override
    public AppMember selectAppMember(Long aid, MemberTypeEnum memberTypeEnum) {
        LambdaQueryWrapper<AppMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppMember::getAid, aid);
        wrapper.eq(AppMember::getType, memberTypeEnum);
        return this.appMemberMapper.selectOne(wrapper);
    }

    @Override
    public void follow(Long followId, int followNum) {
        if (followId == null || followNum == 0) {
            return;
        }

        LambdaUpdateWrapper<AppMember> wrapper = new LambdaUpdateWrapper<>();
        if (followNum > 0) {
            wrapper.setSql("follow_num = follow_num + " + followNum);
        } else {
            wrapper.setSql("follow_num = follow_num - " + Math.abs(followNum));
        }

        wrapper.eq(AppMember::getId, followId);
        this.update(wrapper);
    }
}
